// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
//import VueRouter from 'vue-router'
import Vuex from 'vuex'
import store from './store'
import router from './router'
import App from './App'
import axios from 'axios'
import { ToastPlugin, AlertPlugin,AjaxPlugin,LoadingPlugin} from 'vux'
import 'common/js/const.js'
import {saveLogin,exit} from 'common/js/localStorage.js'
import {checkEmpty} from 'common/js/util.js'

import VueLazyload from 'vue-lazyload'
import Loading from './components/loading/index.js'
Vue.use(Loading);
Vue.use(VueLazyload, {
  loading: require('./assets/default.png')
})

Vue.use(AjaxPlugin)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(LoadingPlugin)

Vue.use(Vuex)

// const store = new Vuex.Store({}) // 这里你可能已经有其他 module

// store.registerModule('vux', { // 名字自己定义
//   state: {
//     isLoading: false
//   },
//   mutations: {
//     updateLoadingStatus (state, payload) {
//       state.isLoading = payload.isLoading
//     }
//   }
// })

// router.beforeEach(function (to, from, next) {
//   store.commit('updateLoadingStatus', {isLoading: true})
//   next()
// })
// router.afterEach(function (to) {
//   store.commit('updateLoadingStatus', {isLoading: false})
// })

axios.interceptors.request.use(function (config) {  //配置发送请求的信息
  store.dispatch('showLoading')  
  //store.commit('updateLoadingStatus', {isLoading: true})
  return config;
}, function (error) {
  return Promise.reject(error);
});

axios.interceptors.response.use(function (response) { //配置请求回来的信息
  store.dispatch('hideLoading')
  //store.commit('updateLoadingStatus', {isLoading: false})
  return response;
}, function (error) {

  return Promise.reject(error);
});

// 网络请求
Vue.prototype.ajaxplugin = function(url,params,successback) {
  store.dispatch('showLoading') 
  this.$http.post(HOST + url, params).then((res) => {
    store.dispatch('hideLoading')
    res = res.data
    let result = res.result
    if (result == 'success') {
      console.log(res.result)
        //this.$vux.toast.text(res.message, 'bottom')
        successback(res)
    } else {
      console.log(res.result)
        this.$vux.alert.show({
            title: '提示',
            content: res.message
        })
    }
  },(err) => {
      this.$vux.alert.show({
          title: '提示',
          content: "请求失败"
      })
  })

}
//弹出框
Vue.prototype.msgalert = function(title, msg) {
  this.$vux.alert.show({
      title: title,
      content: msg
  });
}
//toast
Vue.prototype.msgtoast = function(msg, postion) {
  this.$vux.toast.text(msg, postion)
}
//公共方法
Vue.prototype.saveLogin = saveLogin
Vue.prototype.exit = exit
Vue.prototype.checkEmpty = checkEmpty


FastClick.attach(document.body)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app-box')


